<div class="wmm-module">
    <div class="layui-form-item">
        <form id="search-form" class="layui-form" action="javascript:;">
            <div class="layui-inline">
                <input type="text" name="tableName" autocomplete="off" lay-verify="required" placeholder="表名"
                    class="layui-input">
            </div>
            <div class="layui-inline">
                <button type="submit"  class="layui-btn layui-btn-normal layui-btn-sm datatable-search"><i class="layui-icon layui-icon-search search-icon"></i>
                    查询</button>
            </div>
            <div class="layui-inline">
                <button class="layui-btn layui-btn-normal layui-btn-sm" onclick="renders.box(null,true)">添加多表连接</button>
            </div>    
        </form>
    </div>

    <div class="layui-form-item">
        <form class="layui-form"  action="javascript:;">
            <div class="layui-inline" wmm-fb-auth="DEVELOPER">
                <div class="layui-input-inline">
                    <select name="paramId">
                        <option value="">选择初始化参数</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline" wmm-fb-auth="DEVELOPER">
                <div class="layui-input-inline">
                    <select name="templateGroupId">
                        <option value="">选择初始化模板组</option>
                    </select>
                </div>
            </div>
            <button lay-submit lay-filter="generate" class="layui-btn layui-btn-normal layui-btn-sm">一键生成</button>
        </form>
    </div>

    <div>
        <div id="tb-sys-table-head" lay-filter="tb-sys-table-head">
        </div>
    </div>
</div>


<template id="tmp-param-form">
    <div style="padding:10px">
        <form class="layui-form" lay-filter="param-form" action="javascript:;">
            <input type="hidden" value="" name="id" />
					<div class="layui-form-item">
						<label class="layui-form-label">表名<span class="required-star">*</span></label>
						<div class="layui-input-block">
							<input type="text" name="tableName" autocomplete="off" lay-verify="required" placeholder="请输入表名"
								   class="layui-input">
						</div>
					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">表备注</label>
						<div class="layui-input-block">
							<input type="text" name="tableContent" autocomplete="off"  placeholder="请输入表备注,以英文逗号隔开"
								   class="layui-input">
						</div>
					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">字段名<span class="required-star">*</span></label>
						<div class="layui-input-block">
							<input type="text" name="fieldName" autocomplete="off" lay-verify="required" placeholder="请输入字段名,以英文逗号隔开"
								   class="layui-input">
						</div> 
					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">字段内容</label>
						<div class="layui-input-block">
							<input type="text" name="fieldConent" autocomplete="off"  placeholder="请输入字段内容,以英文逗号隔开"
								   class="layui-input">
						</div>
					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">字段类型<span class="required-star">*</span></label>
						<div class="layui-input-block">
							<input type="text" name="fieldType" autocomplete="off" lay-verify="required" placeholder="请输入字段类型,以英文逗号隔开"
								   class="layui-input">
						</div>
					</div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button type="button" class="layui-btn lay-submit layui-btn-normal" lay-submit lay-filter="resource">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
    </div>
</template>



<script>

    new Wmm({
        vars: {
            table: {},
            appList: undefined,
            reqUrl: "/sysbeetlelinkedlist/",
            templateGroupUrl: "/sysbeetletemplategroup/all",
            paramUrl: "/sysbeetleparams/all",
            generateUrl: "/sysbeetlelinkedlist/generator"
        },

        imports: ["form","api","datatable","req","sysTools"],
        listener: function () {

            fb.form.on('submit(resource)', function (data) {
                var dataObject = data.field;
                console.log("========dataObject=============:"+JSON.stringify(dataObject))
                if (dataObject.id) {
                    req.put(vars.reqUrl,dataObject,function(ret){
                            layer.closeAll();
                            layer.msg("修改成功",{icon:6});
                            vars.table.flush();
                    })
                    return;
                }
                //insert
                console.log("=========dataObject========="+dataObject);
                req.post(vars.reqUrl,dataObject,function(data){
                    console.log("======进入成功======");
                    console.log("data:"+JSON.stringify(data));
					layer.closeAll();
					layer.msg("添加成功",{icon:6});
					vars.table.flush();
                })
            });

            fb.form.on("submit(generate)",function(data){
              var generation = data.field;
              var linkedListIds = vars.table.getCheckedData();
              generation.linkedListIds = linkedListIds;
              console.log("======>"+JSON.stringify(generation))
              req.post(vars.generateUrl,generation,function(data){
                if(typeof(data.code) == 'undefined'){
                    layer.msg("一键生成成功",{icon:6});
                  }
                  console.log("=============成功==============");
              },function(ret){
                  console.error("============失败================")
              })
              return false;
              // req.get()
          })
        },
        requests: {
            
        },
        renders: {
            box: function (data, isCreate) {
                layer.open({
                    type: 1,
                    title: (isCreate ? "添加" : "修改") + "多表连接",
                    content: document.querySelector("#tmp-param-form").innerHTML,
                    area: ['550px', '760px'],
                    success: function () {
                        if (!isCreate) {
                            //表单初始赋值
                            fb.form.val('param-form', data)
                        }
                        fb.form.render();
                    }
                });
            },
            render: function () {

                //模板组
                req.get(vars.templateGroupUrl,function(data){
                    fb.utils.render.select("[name=templateGroupId]",data,"groupName","id");
                })
                
                //参数组
                req.get(vars.paramUrl,function(data){
                    fb.utils.render.select("[name=paramId]",data,"alias","id");
                })

                vars.table = new DataTable({
                    el: "tb-sys-table-head",
                    formEl: "search-form",
                    url: vars.reqUrl,
                    multiple:true,
                    fixHeightNumber:300,
                    defines: [
							
								{
									head: "Id",
									prop: "id",
									wordBreak:true,
								},
							
								{
									head: "表名",
									prop: "tableName",
									wordBreak:true,
								},
							
								{
									head: "表备注",
									prop: "tableContent",
									wordBreak:true,
								},
							
								{
									head: "字段名",
									prop: "fieldName",
									wordBreak:true,
								},
							
								{
									head: "字段内容",
									prop: "fieldConent",
									wordBreak:true,
								},
							
								{
									head: "字段类型",
									prop: "fieldType",
									wordBreak:true,
								},
							
								{
									head: "创建时间",
									prop: "gmtCreate",
									type: "datetime"
								},
							
								{
									head: "修改时间",
									prop: "gmtModified",
									type: "datetime"
								},
                    ],
                    handles: [
                        {
                            edit: function (data) {
                                return renders.box(data, false);
                            }
                        },
                        {
                            del: {
                                tip: "确定要删除吗？",
                                do: function (data) {
                                    req.delete(vars.reqUrl+data.id,function(data){
                                        console.log("======进入成功======");
                                        console.log("data:"+JSON.stringify(data));
										layer.msg("删除成功",{icon:6});
										vars.table.flush();
                                        
                                    })
                                }
                            }
                        }

                    ],
                });

            },
        },

    });



</script>